define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            $('.btn-add').data('area', ['100%', '100%']);
            $('.btn-edit').data('area', ['100%', '100%']);
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'activity/goods/index' + location.search,
                    add_url: 'activity/goods/add',
                    edit_url: 'activity/goods/edit',
                    del_url: 'activity/goods/del',
                    multi_url: 'activity/goods/multi',
                    import_url: 'activity/goods/import',
                    table: 'goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'type', title: __('Type'),
                            searchList: {1: __('Type One'), 2: __('Type Two')},
                            formatter: Table.api.formatter.label,
                        },
                        {
                            field: 'cate_name',
                            title: __('所属分类'),
                            operate: false,
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, index) {
                                if (value) {
                                    var array = [];
                                    var cateNameArray = value.split(',');
                                    for (var i = 0; i < cateNameArray.length; i++) {
                                        array.push('<span class="label bg-purple">' + cateNameArray[i] + '</span>');
                                    }
                                    return array.join('<br>');
                                }
                                return value;
                            }
                        },
                        {field: 'method_init', title: __('method_init'),
                            searchList:{1: __('method_init_one'), 2: __('method_init_two')},
                            formatter: Table.api.formatter.label
                        },
                        {field: 'method', title: __('Method'),
                            searchList:{3: '参与报名后领取', 2: '参与报名后按打卡次数领取'},
                            formatter: Table.api.formatter.label
                        },
                        {field: 'is_superposition', title: __('is_superposition'),
                            searchList:{1: __('is_superposition_one'), 2: __('is_superposition_two')},
                            formatter: Table.api.formatter.label
                        },
                        {field: 'goods_name', title: __('Goods_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'goods_images', title: __('Goods_images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'goods_desc', title: __('Goods_desc'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'goods_number', title: __('Goods_number')},
                        {field: 'weight', title: __('权重')},
                        {
                            field: 'status', title: __('Status'),
                            searchList: {1: __('Status One'), 2: __('Status Two')},
                            formatter: Table.api.formatter.toggle,
                        },
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            table.on('post-body.bs.table',function(){
                $(".btn-editone").data("area",["100%","100%"]);
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                var methodChecked = parseInt($("input[name='row[method]']:checked").val());
                if( methodChecked === 3) {
                    $('.c-punch_number').css('display', 'none');
                }

                var typeChecked = parseInt($("input[name='row[type]']:checked").val());
                if( typeChecked === 2) {
                    $('.c-cate_ids').css('display', 'none');
                }


                $("input[name='row[method]']").click(function () {
                    if($(this).val() == '3') {
                        $('.c-punch_number').css('display', 'none');
                    } else {
                        $('.c-punch_number').css('display', 'block');
                    }
                    $('#c-punch_number_min').val('');
                    $('#c-punch_number_max').val('');
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
